#include "../include/head.h"
#include <stdlib.h>
#include <stdio.h>

// 创建新节点
ListNode* create_node(void *data) {
    ListNode *new_node = (ListNode*)malloc(sizeof(ListNode));
    if (new_node == NULL) {
        return NULL;
    }
    new_node->data = data;
    new_node->next = NULL;
    return new_node;
}

// 尾插法
void insert_at_end(ListNode **head, void *data) {
    ListNode *new_node = create_node(data);
    if (new_node == NULL) return;
    
    if (*head == NULL) {
        *head = new_node;
        return;
    }
    
    ListNode *current = *head;
    while (current->next != NULL) {
        current = current->next;
    }
    current->next = new_node;
}

// 头插法
void insert_at_begin(ListNode **head, void *data) {
    ListNode *new_node = create_node(data);
    if (new_node == NULL) return;
    
    new_node->next = *head;
    *head = new_node;
}

// 在指定节点后插入
void insert_after(ListNode *prev_node, void *data) {
    if (prev_node == NULL) return;
    
    ListNode *new_node = create_node(data);
    if (new_node == NULL) return;
    
    new_node->next = prev_node->next;
    prev_node->next = new_node;
}

// 删除指定节点
void delete_node(ListNode **head, void *target_data, int (*compare)(void*, void*)) {
    if (*head == NULL || compare == NULL) return;
    
    // 如果头节点就是要删除的节点
    if (compare((*head)->data, target_data) == 0) {
        ListNode *temp = *head;
        *head = (*head)->next;
        free(temp);
        return;
    }
    
    // 查找要删除的节点
    ListNode *current = *head;
    while (current->next != NULL && compare(current->next->data, target_data) != 0) {
        current = current->next;
    }
    
    // 找到要删除的节点
    if (current->next != NULL) {
        ListNode *temp = current->next;
        current->next = temp->next;
        free(temp);
    }
}

// 删除头节点
void delete_head(ListNode **head) {
    if (*head == NULL) return;
    
    ListNode *temp = *head;
    *head = (*head)->next;
    free(temp);
}

// 删除尾节点
void delete_tail(ListNode **head) {
    if (*head == NULL) return;
    
    // 如果只有一个节点
    if ((*head)->next == NULL) {
        free(*head);
        *head = NULL;
        return;
    }
    
    // 找到倒数第二个节点
    ListNode *current = *head;
    while (current->next->next != NULL) {
        current = current->next;
    }
    
    free(current->next);
    current->next = NULL;
}

// 释放整个链表（不释放数据）
void free_list(ListNode *head) {
    ListNode *current = head;
    while (current != NULL) {
        ListNode *temp = current;
        current = current->next;
        free(temp);
    }
}

// 释放整个链表（同时释放数据）
void free_list_with_data(ListNode *head, void (*free_data)(void*)) {
    ListNode *current = head;
    while (current != NULL) {
        ListNode *temp = current;
        current = current->next;
        if (free_data != NULL) {
            free_data(temp->data);
        }
        free(temp);
    }
}

// 获取链表长度
int get_list_length(ListNode *head) {
    int count = 0;
    ListNode *current = head;
    while (current != NULL) {
        count++;
        current = current->next;
    }
    return count;
}

// 遍历链表
void traverse_list(ListNode *head, void (*process)(void*)) {
    ListNode *current = head;
    while (current != NULL) {
        if (process != NULL) {
            process(current->data);
        }
        current = current->next;
    }
    printf("NULL\n");
}

// 查找节点
ListNode* find_node(ListNode *head, void *target_data, int (*compare)(void*, void*)) {
    if (head == NULL || compare == NULL) return NULL;
    
    ListNode *current = head;
    while (current != NULL) {
        if (compare(current->data, target_data) == 0) {
            return current;
        }
        current = current->next;
    }
    return NULL;
}